﻿@page "/unit"

@using System.Collections.ObjectModel
@inject DialogService DialogService
@inject ProductService ProductService
@inject ProductUnitService ProductUnitService

<RadzenText TextStyle="TextStyle.H4">单位管理</RadzenText>
<PageTitle>单位管理</PageTitle>
<RadzenStack Style="display:flex" Orientation="Orientation.Horizontal" JustifyContent="JustifyContent.Start">
    <RadzenTextBox @bind-Value="@keyword" Placeholder="单位名称" Style="margin:10px 0px 10px 0px" />
    <RadzenButton Text="查询" Icon="refresh" Click=@(args => Search()) ButtonStyle="ButtonStyle.Primary" Style="margin:10px 0px 10px 0px" />
    <RadzenButton Text="新增" Icon="add_circle" Click=@(args => Add()) ButtonStyle="ButtonStyle.Primary" Style="margin:10px 0px 10px 0px" />
</RadzenStack>
<RadzenDataGrid Data="@units" EmptyText="暂无数据" GridLines="Radzen.DataGridGridLines.Default" AllowPaging="true" AllowSorting="true">
    <Columns>
        <RadzenDataGridColumn Property="Id" Title="序号" Width="100px" TextAlign="TextAlign.Center" />
        <RadzenDataGridColumn Property="" Title="操作" Width="260px">
            <Template Context="data">
                <RadzenStack Gap="10" Style="margin-bottom: 10px" Orientation="Orientation.Horizontal">
                    <RadzenButton Text="修改" @onclick:stopPropagation Click="@(args=> Update(data))" ButtonStyle="ButtonStyle.Primary" />
                    <RadzenButton Text="删除" @onclick:stopPropagation Click="@(args=> Delete(data))" ButtonStyle="ButtonStyle.Danger" />
                </RadzenStack>
            </Template>
        </RadzenDataGridColumn>
        <RadzenDataGridColumn Property="Name" Title="名称" />
    </Columns>
    <LoadingTemplate>
        <RadzenProgressBarCircular ProgressBarStyle="ProgressBarStyle.Primary" Value="100" ShowValue="false" Mode="ProgressBarMode.Indeterminate" />
    </LoadingTemplate>
</RadzenDataGrid>

@code {
    string keyword = string.Empty;
    ObservableCollection<Unit>? units;

    async Task Search()
    {
        await Task.Run(Query);
    }

    private void Query()
    {
        var input = new BaseInput()
            {
                keyword = keyword,
            };
        var list = ProductUnitService.Search(input);
        units = new ObservableCollection<Unit>(list);
    }

    private async void Add()
    {
        var unit = new Unit();
        var result = await DialogService.OpenAsync<UnitAddDialog>($"新增",
               new Dictionary<string, object>() {{ "PUnit", unit}},
               new DialogOptions()
                   {
                       Resizable = true,
                       Draggable = true,
                       Width = "700px",
                       Height = "512px",
                       Left = null,
                       Top = null
                   });
        if (result != null)
        {
            units!.Add(result);
        }
    }

    private async void Update(Unit selectedItem)
    {
        Unit result = await DialogService.OpenAsync<UnitAddDialog>($"修改",
                      new Dictionary<string, object>() {{ "PUnit", selectedItem }},
                      new DialogOptions()
                          {
                              Resizable = true,
                              Draggable = true,
                              Width = "700px",
                              Height = "512px",
                              Left = null,
                              Top = null
                          });
        if(result != null){
            StateHasChanged();
        }
    }

    private async void Delete(Unit selectedItem)
    {
        bool? result = await DialogService.Confirm("确认要删除吗?", "提示", new ConfirmOptions()
            {
                OkButtonText = "确定",
                CancelButtonText = "取消"
            });

        if ((bool)result!)
        {
            try
            {
                ProductUnitService.Delete(selectedItem.Id);
                units!.Remove(selectedItem);
            }
            catch (Exception e)
            {
                await DialogService.Alert(e.Message, "错误", new AlertOptions() { OkButtonText = "确定" });
            }
        }
    }

    protected override async Task OnInitializedAsync()
    {
        await Task.Run(() =>
        {
            var list = ProductUnitService.Search(new BaseInput());
            units = new ObservableCollection<Unit>(list);
        });
    }
}
